package com.softstao.guoyu.ui.activity.sale;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseActivity;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.model.sale.ProvinceRebateCondition;
import com.softstao.guoyu.model.sale.ProvinceRebateDetails;
import com.softstao.guoyu.model.sale.RebateResult;
import com.softstao.guoyu.mvp.interactor.sale.RetailInteractor;
import com.softstao.guoyu.mvp.presenter.sale.RetailPresenter;
import com.softstao.guoyu.mvp.viewer.sale.ProvinceRebateDetailsViewer;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewBaseAdapter;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewHolder;
import com.softstao.softstaolibrary.library.utils.LZUtils;
import com.softstao.softstaolibrary.library.widget.CustomScrollerView;
import com.softstao.softstaolibrary.library.widget.EmptyLayout;
import com.softstao.softstaolibrary.library.widget.FullyLinearLayoutManager;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ProvinceRebateDetailsActivity extends BaseActivity implements ProvinceRebateDetailsViewer {

    @AIPresenter(presenter = RetailPresenter.class, interactor = RetailInteractor.class)
    RetailPresenter presenter;

//    @BindView(R.id.tab_strip)
//    CustomTabStrip tabStrip;
    @BindView(R.id.time_view)
    TextView timeView;
    @BindView(R.id.list)
    RecyclerView listView;
    @BindView(R.id.more)
    TextView more;
    @BindView(R.id.scroll_view)
    CustomScrollerView scrollView;
    @BindView(R.id.empty_layout)
    EmptyLayout emptyLayout;
    @BindView(R.id.loading)
    RelativeLayout loading;
    @BindView(R.id.sum_money)
    TextView sumMoney;

    private ProvinceRebateCondition condition = new ProvinceRebateCondition();
    private String method = "";
    private RecycleViewBaseAdapter<RebateResult> adapter;
    private List<RebateResult> list = new ArrayList<>();
    private String time;

    @Override
    public int _ContentView() {
        return R.layout.activity_province_rebate_details;
    }

    @Override
    public void initView() {
//        initTitle("返利明细");
        scrollView.setOnScrollChangedListener(this);
        setPtrFrameLayoutEnable();

        method = getIntent().getStringExtra("method");
        time = getIntent().getStringExtra("time");

        condition.setAgentId(SharePreferenceManager.getInstance().getAgentId());
        condition.setWay(method);
        condition.setMonth(time.replace("年", "-").replace("月", ""));
        timeView.setText(time + "返利明细");

        switch (method) {
            case "oneLevel":
                initTitle("一级省代返利");
                break;
            case "twoLevel":
                initTitle("二级省代返利");
                break;
            case "monthManage":
                initTitle("月度管理奖励");
                break;
        }

//        tabStrip.setTitles("一级省代返利", "二级省代返利");
//        switch (method) {
//            case "oneLevel":
//                tabStrip.setTabIndex(0);
//                break;
//            case "twoLevel":
//                tabStrip.setTabIndex(1);
//                break;
//        }
//        tabStrip.setOnTabStripSelectedIndexListener(new NavigationTabStrip.OnTabStripSelectedIndexListener() {
//            @Override
//            public void onStartTabSelected(String title, int index) {
//            }
//
//            @Override
//            public void onEndTabSelected(String title, int index) {
//                switch (title) {
//                    case "一级省代返利":
//                        method = "oneLevel";
//                        break;
//                    case "二级省代返利":
//                        method = "twoLevel";
//                        break;
//                }
//                condition.setWay(method);
//                currentPage = 1;
//                findRebates();
//            }
//        });

        adapter = new RecycleViewBaseAdapter<RebateResult>(list, R.layout.layout_rebate_list_item) {
            @Override
            public void convert(RecycleViewHolder holder, RebateResult rebateResult) {
                RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                holder.itemView.setLayoutParams(layoutParams);
                if (rebateResult.getAvatar() != null && !rebateResult.getAvatar().equals("")) {
                    Glide.with(mContext).load(rebateResult.getAvatar()).into((ImageView) holder.getView(R.id.avatar));
                } else {
                    ((ImageView) holder.getView(R.id.avatar)).setImageResource(R.mipmap.default_avatar);
                }
                holder.setText(R.id.name, rebateResult.getName())
                        .setText(R.id.level, rebateResult.getLevel())
                        .setText(R.id.mobile, rebateResult.getMobile())
                        .setText(R.id.money, getResources().getString(R.string.rmb) + LZUtils.priceFormat(rebateResult.getMoney()));
                ((TextView) holder.getView(R.id.money)).setCompoundDrawables(null, null, null, null);
            }
        };
        listView.setAdapter(adapter);
        listView.setLayoutManager(new FullyLinearLayoutManager(this));
    }

    @Override
    protected void onResume() {
        super.onResume();
        switch (method) {
            case "oneLevel":
                findRebates();
                break;
            case "twoLevel":
                findRebates();
                break;
            case "monthManage":
                findMonthManage();
                break;
        }
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        switch (method) {
            case "oneLevel":
                findRebates();
                break;
            case "twoLevel":
                findRebates();
                break;
            case "monthManage":
                findMonthManage();
                break;
        }
    }

    @Override
    public void onLoad() {
        super.onLoad();
        switch (method) {
            case "oneLevel":
                findRebates();
                break;
            case "twoLevel":
                findRebates();
                break;
            case "monthManage":
                findMonthManage();
                break;
        }
    }

    @Override
    public void findRebates() {
        loading.setVisibility(View.VISIBLE);
        presenter.getProvinceRebateDetails(currentPage, condition);
    }

    @Override
    public void findMonthManage() {
        loading.setVisibility(View.VISIBLE);
        presenter.getMonthManageDetails(currentPage, SharePreferenceManager.getInstance().getAgentId(), time.replace("年", "-").replace("月", ""));
    }

    @Override
    public void getRebates(ProvinceRebateDetails provinceRebateDetails) {
        loading.setVisibility(View.GONE);
        if (provinceRebateDetails != null) {
            switch (method) {
                case "oneLevel":
                    sumMoney.setText("一级销售总和：" + provinceRebateDetails.getOneSumMoney() + "元");
                    break;
                case "twoLevel":
                    sumMoney.setText("二级销售总和：" + provinceRebateDetails.getTwoSumMoney() + "元");
                    break;
            }
            if (provinceRebateDetails.getResultOneLevel() != null && provinceRebateDetails.getResultOneLevel().size() != 0) {
                emptyLayout.setVisibility(View.GONE);
                more.setVisibility(View.GONE);
                if (currentPage == 1) {
                    list.clear();
                }
                list.addAll(provinceRebateDetails.getResultOneLevel());
                adapter.notifyDataSetChanged();
            } else {
                if (currentPage == 1) {
                    list.clear();
                    adapter.notifyDataSetChanged();
                    emptyLayout.setVisibility(View.VISIBLE);
                } else {
                    more.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    @Override
    public void getMonthManage(ProvinceRebateDetails provinceRebateDetails) {
        loading.setVisibility(View.GONE);
        if (provinceRebateDetails != null) {
            sumMoney.setText("直属团队销售总和：" + provinceRebateDetails.getSumMoney() + "元");
            if (provinceRebateDetails.getMonthsManageList() != null && provinceRebateDetails.getMonthsManageList().size() != 0) {
                emptyLayout.setVisibility(View.GONE);
                more.setVisibility(View.GONE);
                if (currentPage == 1) {
                    list.clear();
                }
                list.addAll(provinceRebateDetails.getMonthsManageList());
                adapter.notifyDataSetChanged();
            } else {
                if (currentPage == 1) {
                    list.clear();
                    adapter.notifyDataSetChanged();
                    emptyLayout.setVisibility(View.VISIBLE);
                } else {
                    more.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    @Override
    protected void error(String msg) {
        super.error(msg);
        loading.setVisibility(View.GONE);
    }
}
